<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="profile-header">
          <div class="avatar-container">
            <el-avatar 
              :size="100" 
              class="profile-avatar" 
              :class="{ 'avatar-shake': isEditing }"
            >
              {{ username.charAt(0).toUpperCase() }}
            </el-avatar>
            <div class="level-badge">LV.{{ level }}</div>
          </div>
          <h2>{{ username }}</h2>
          <div class="user-status">{{ email }}</div>
        </div>
      </template>
      
      <div class="profile-info">
        <div class="stats-container">
          <div class="stat-item">
            <div class="stat-icon experience">
              <el-icon><Star /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-label">等级经验</div>              <div class="exp-wrapper">
                <el-progress 
                  :percentage="expPercentage" 
                  :stroke-width="12"
                  class="exp-progress"
                />
                <span class="exp-text">{{ user?.exp || 0 }}/{{ user?.nextLevelExp || 100 }}</span>
              </div>
            </div>
          </div>
        </div>

        <div class="form-section">
          <h3>修改用户名</h3>
          <el-form 
            ref="formRef"
            :model="form"
            :rules="rules"
            label-width="0"
            class="profile-form"
          >
            <el-form-item prop="username" class="username-item">
              <div class="username-input-wrapper" :class="{ 'is-editing': isEditing }">
                <el-input 
                  v-model="form.username" 
                  placeholder="请输入新的用户名"
                  maxlength="10"
                  show-word-limit
                  @focus="isEditing = true"
                  @blur="isEditing = false"
                >
                  <template #prefix>
                    <el-icon><User /></el-icon>
                  </template>
                </el-input>
              </div>
            </el-form-item>
              
            <el-form-item class="button-item">
              <div class="button-wrapper">
                <el-button 
                  type="primary" 
                  @click="handleSubmit"
                  :loading="loading"
                  class="save-button"
                  :class="{ 'button-pulse': form.username !== username }"
                >
                  <el-icon><Check /></el-icon>
                  保存修改
                </el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { User, Check, Star } from '@element-plus/icons-vue'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { calculateExpPercentage } from '../utils/helpers'

export default {
  name: 'ProfileView',
  components: {
    User,
    Check,
    Star
  },
  setup() {
    const userStore = useUserStore();
    const formRef = ref();
    const loading = ref(false);
    const isEditing = ref(false);

    const user = computed(() => userStore.user),
          username = computed(() => user.value?.username || '用户'),
          email = computed(() => user.value?.email || ''),
          level = computed(() => user.value?.level || 1),
          expPercentage = computed(() => 
            calculateExpPercentage(user.value?.exp || 0, user.value?.nextLevelExp || 100)
          );

    const form = reactive({
      username: user.value?.username || ''
    });

    const validateUsername = (_, value, callback) => {
      const usernameRegex = /^[a-zA-Z0-9\u4e00-\u9fa5_-]{1,10}$/;
      if (!value) {
        callback(new Error('用户名不能为空'));
      } else if (value.length < 1 || value.length > 10) {
        callback(new Error('用户名长度必须在1-10个字符之间'));
      } else if (!usernameRegex.test(value)) {
        callback(new Error('用户名只能包含字母、数字、中文、下划线和短横线'));
      } else {
        callback();
      }
    };

    const rules = {
      username: [
        { required: true, validator: validateUsername, trigger: 'blur' }
      ]
    };    // Remove unused expFormat function

    const handleSubmit = async () => {
      if (!formRef.value) return;

      try {
        await formRef.value.validate();
        ElMessage({ 
          message: '更新成功',
          type: 'success',
          duration: 2000,
          showClose: false
        });
      } catch (error) {
        console.error('Update profile error:', error);
        ElMessage.error('更新失败');
      }
    };    return {
      formRef,
      form,
      rules,
      loading,
      username,
      email,
      level,
      expPercentage,
      isEditing,      handleSubmit,
      user  // Add user to the returned properties
    };
  }
}
</script>

<style scoped>
.profile-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.profile-card {
  border-radius: 20px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  overflow: visible;
}

.profile-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
}

.profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px 0;
  position: relative;
  background: linear-gradient(135deg, rgba(25, 137, 250, 0.1), rgba(94, 114, 228, 0.1));
}

.avatar-container {
  position: relative;
  margin-bottom: 20px;
}

.profile-avatar {
  background: linear-gradient(135deg, #1989fa 0%, #5e72e4 100%);
  font-size: 42px;
  font-weight: bold;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border: 4px solid #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.profile-avatar:hover {
  transform: scale(1.05) rotate(5deg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.avatar-shake {
  animation: shake 0.5s cubic-bezier(0.36, 0, 0.66, -0.56);
}

.level-badge {
  position: absolute;
  bottom: -5px;
  right: -5px;
  background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: bold;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  border: 2px solid #fff;
}

.profile-header h2 {
  font-size: 28px;
  background: linear-gradient(135deg, #1989fa, #5e72e4);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 0 0 8px 0;
}

.user-status {
  color: #606266;
  font-size: 14px;
  opacity: 0.8;
}

.stats-container {
  padding: 20px;
  margin-bottom: 20px;
  background: rgba(25, 137, 250, 0.03);
  border-radius: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 10px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stat-icon.experience {
  background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
}

.stat-content {
  flex: 1;
}

.stat-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.exp-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
}

.exp-progress {
  flex: 1;
}

.exp-progress :deep(.el-progress-bar__outer) {
  border-radius: 6px;
  background-color: rgba(25, 137, 250, 0.1);
}

.exp-progress :deep(.el-progress-bar__inner) {
  border-radius: 6px;
  background: linear-gradient(90deg, #f6d365, #fda085);
  transition: all 0.3s ease;
}

.exp-progress :deep(.el-progress__text) {
  color: #606266;
  font-size: 13px;
}

.exp-text {
  font-size: 13px;
  color: #606266;
  white-space: nowrap;
}

.form-section {
  padding: 20px;
}

.form-section h3 {
  font-size: 18px;
  color: #2c3e50;
  margin: 0 0 20px 0;
  font-weight: 600;
}

.profile-form {
  max-width: 400px;
  margin: 0 auto;
}

.username-input-wrapper {
  position: relative;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.username-input-wrapper.is-editing {
  transform: scale(1.02) translateY(-2px);
}

.username-item {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.username-item :deep(.el-form-item__label) {
  font-size: 15px;
  color: #606266;
  font-weight: 500;
  padding: 0 8px 0 0 !important;
  height: 42px;
  line-height: 42px;
  text-align: right;
  box-sizing: border-box;
}

.username-item :deep(.el-form-item__content) {
  margin-left: 0 !important;
  flex: 1;
}

.username-item :deep(.el-input__wrapper) {
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 2px solid transparent;
  background: linear-gradient(#fff, #fff) padding-box,
              linear-gradient(135deg, #1989fa20, #5e72e420) border-box;
  height: 42px;
}

.username-item :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px rgba(25, 137, 250, 0.1);
  border-color: #1989fa;
  transform: translateY(-1px);
}

.username-item :deep(.el-input__inner) {
  font-size: 15px;
  color: #2c3e50;
  height: 42px;
  line-height: 42px;
}

.username-item :deep(.el-input__prefix) {
  color: #1989fa;
  font-size: 16px;
  line-height: 42px;
}

.button-item {
  margin-bottom: 20px;
}

.button-item :deep(.el-form-item__content) {
  margin-left: 60px !important;
}

.button-wrapper {
  margin-top: 20px;
}

.save-button {
  width: 100%;
  height: 42px;
  border-radius: 12px;
  font-weight: 500;
  letter-spacing: 0.5px;
  background: linear-gradient(135deg, #1989fa, #5e72e4);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  padding: 0 15px;
  font-size: 15px;
}

.save-button :deep(.el-icon) {
  font-size: 16px;
  margin-right: 2px;
}

.save-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(25, 137, 250, 0.3);
}

.save-button:active {
  transform: translateY(0);
}

.button-pulse {
  animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes shake {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-3deg) scale(1.05); }
  75% { transform: rotate(3deg) scale(1.05); }
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(25, 137, 250, 0.4);
  }
  70% {
    box-shadow: 0 0 0 15px rgba(25, 137, 250, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(25, 137, 250, 0);
  }
}

.info-item {
  margin: 15px 0;
  padding: 10px;
  background: rgba(64, 158, 255, 0.05);
  border-radius: 8px;
  transition: all 0.3s ease;
}

.info-item:hover {
  background: rgba(64, 158, 255, 0.1);
}

.info-label {
  color: #666;
  font-size: 14px;
  margin-bottom: 5px;
}

.info-value {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.info-divider {
  margin: 20px 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(0,0,0,0.1), transparent);
}
</style>
